<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="800" destroy-on-close @closed="$emit('closed')" :align-center="true" :close-on-click-modal="false">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="110px" label-position="right">
            <el-form-item label="供应商类型">
                <el-select v-model="apply_cate" placeholder="请选择供应商类型" style="width:100%" @change="change">
                    <el-option label="工厂供应商" :value="1"></el-option>
                    <el-option label="工序供应商" :value="2"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="供应商" prop="apply_ids">
                <el-select v-model="form.apply_ids" placeholder="请选择供应商" style="width:100%" multiple>
                    <el-option v-for="item in waitList" :key="item.apply_id" :label="item.referred_as" :value="item.apply_id"/>
                </el-select>
            </el-form-item>
		</el-form>
		<template #footer>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">确 定</el-button>
			<el-button @click="visible=false" >取 消</el-button>
		</template>
	</el-dialog>
</template>

<script>
	export default {
		emits: ['success', 'closed'],
		data() {
			return {
				mode: "add",
				titleMap: {
					add: '绑定供应商',
					edit: '编辑客户',
					show: '查看'
				},
				visible: false,
				isSaveing: false,
                grade:"潜力",
				//表单数据
				form: {
                    apply_ids:[],
				},
                apply_cate:"",
				//验证规则
				rules: {
					code: [
						{required: true, message: '请输入仓库编码'}
					],
					name: [
						{required: true, message: '请输入仓库名称'}
					],
				},
                waitList:[],
			}
		},
		mounted() {
		},
		methods: {
			//显示
			open(mode='add'){
				this.mode = mode;
				this.visible = true;
				return this
			},
			//表单提交方法
			submit(){
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid) {
						this.isSaveing = true;
						var res  = [];
						// if (this.form.id) {
                        // }else{
                        // res = await this.$API.basic.apply.add.post(this.form);
                        // }
                        res = await this.$API.basic.apply.bindApply.post(this.form);
						this.isSaveing = false;
						if(res.code == 1){
							this.$emit('success', this.form, this.mode)
							this.visible = false;
							this.$message.success("操作成功")
						}else{
							this.$alert(res.message, "提示", {type: 'error'})
						}
					}else{
						return false;
					}
				})
			},
			//表单注入数据
			setData(data){
                console.log('data :>> ', data);
			},
            async change(e){
                var res = await this.$API.basic.apply.whole.get({type:e});
                this.waitList = res.data
                let number = []
                for(var i = 0; i < res.data.length; i++) {
                    var data = res.data[i].is_exist;
                    if(data == 1 ) {
                        number.push(res.data[i].apply_id)
                    }
                }
                this.form.apply_ids = number
            },
		}
	}
</script>

<style lang="scss">
.el-form-item--default{
    align-items: center;
}
</style>
